package com.sc.nft.callback;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.mate.sdk.Callback;
import com.mate.sdk.domain.NFTNotify;
import com.mate.sdk.domain.Notify;
import com.sc.nft.entity.ChainCallBackLog;
import com.sc.nft.enums.*;
import com.sc.nft.util.WeChatUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

/**
 * 交易回调
 */
@Component
@Scope("prototype")
@RequiredArgsConstructor
@Slf4j
public class TransferFromMetaWalletCallBack implements Callback {



    @Override
    @Transactional(rollbackFor = Exception.class)
    public void callback(Notify notify) {
        NFTNotify nftNotify = (NFTNotify) notify;
        if(!notify.isSuccess()){
            WeChatUtil.sendWeChatMessage(StrUtil.format("NFT跨链转入处理失败(TransferFromMetaWalletCallBack),错误信息:{},交易Hash:{}", nftNotify.getMessage(),nftNotify.getTransactionHash()));
        }
        ChainCallBackLog chainCallBackLog = new ChainCallBackLog();
        chainCallBackLog.setCallBackType(ChainCallBackTypeEnum.TRANSFER_FROM_META_WALLET);
        chainCallBackLog.setTransactionHash(nftNotify.getTransactionHash());
        chainCallBackLog.setTokenId(nftNotify.getTokenId());
        chainCallBackLog.setIsSuccess(nftNotify.isSuccess());
        chainCallBackLog.setMessage(nftNotify.getMessage());
        chainCallBackLog.setStatus(ChainCallBackStatusEnum.TO_BE_PROCESSED);
        chainCallBackLog.setResultJson(JSONUtil.toJsonStr(nftNotify));
        chainCallBackLog.insert();
    }
}
